<?php

namespace admin\widgets;

use Yii;
use yii\base\Widget;

/**
 * 菜单部件
 */
class MenuWidget extends Widget
{

    /**
     * Executes the widget.
     */
    public function run()
    {
        return $this->render('menu', ['menus' => $this->getMenus()]);
    }

    private function getMenus()
    {
        $menus = Yii::$app->params['menus'];

        $route = Yii::$app->requestedRoute;
        foreach ($menus as &$menu) {
            foreach ($menu['children'] as &$menu1) {
                if ($menu1['route'] == $route) {
                    $menu['class'] = 'active open';
                    $menu1['class'] = 'active open';
                    break 2;
                }
            }
        }

        return $menus;
    }
}
